//创建一个页面
import 'package:flutter/material.dart';
import 'package:flutter_worker730/page/404.dart';
import 'package:flutter_worker730/page/house/building_list.dart';
import 'package:flutter_worker730/page/house/house_detail.dart';
import 'package:flutter_worker730/page/house/house_form.dart';
import 'package:flutter_worker730/page/house/house_list.dart';
import 'package:flutter_worker730/page/house/location_list.dart';
import 'package:flutter_worker730/page/house/room_list.dart';
import 'package:flutter_worker730/page/login/index.dart';
import 'package:flutter_worker730/page/notice/notice_datail.dart';
import 'package:flutter_worker730/page/profile/profile_page.dart';
import 'package:flutter_worker730/page/tabs_page.dart';
import 'package:flutter_worker730/utils/TokenManager.dart';

void main(List<String> args) {
  // 初始化token
  tokenManager.initToken();
  runApp(MaterialApp(
    //配置路由表
    routes: {
      "/": (context) => const TabsPage(),
      "/details": (context) => const Text("详情"),
      '/notice_detail': (context) => NoticeDetail(),
      '/login': (context) => const LoginPage(),
    },
    // 路由拦截不允许使用async标记(不允许有异步逻辑  耗时方法)
    // getToken必须处理成同步的!!!!
    onGenerateRoute: (RouteSettings settings) {
      // 又是一个新来的保安  啥也不干

      // 分析: 判断有无token 有token就进去  没token就跳转到登录页面
      // tokenManager.getToken().then((value) => null)
      // var token = tokenManager.getToken();

      print(settings.name); // 跳转去的路由名
      print(settings.arguments); // 跳转去的路由携带的参数
      // 分析: 判断有无token 有token就进去  没token就跳转到登录页面
      // tokenManager.getToken().then((value) => null)
      var token = tokenManager.getToken();
      // var token = tokenManager.getToken();
      if (token != '') {
        if (settings.name == '/profile') {
          return MaterialPageRoute(
            // builder: (context) => ProfilePage(),
            builder: (context) => ProfilePage(
                userInfo: settings.arguments as Map<String, dynamic>), // 父传子
          );
        } else if (settings.name == '/house') {
          return MaterialPageRoute(
            builder: (context) => HouseList(),
          );
        } else if (settings.name == '/add_house') {
          return MaterialPageRoute(
            // builder: (context) => Text('add'),
            builder: (context) => LocationList(),
          );
        } else if (settings.name == '/building_list') {
          return MaterialPageRoute(
            // builder: (context) => Text('add'),
            builder: (context) => BuildingList(
                point: (settings.arguments as Map<String, dynamic>)['point']),
          );
        } else if (settings.name == '/room_list') {
          return MaterialPageRoute(
              builder: (context) => RoomList(
                  point: (settings.arguments as Map<String, dynamic>)["point"]
                      as String,
                  building: (settings.arguments
                      as Map<String, dynamic>)["build"] as String));
        } else if (settings.name == '/house_form') {
          return MaterialPageRoute(
            // builder: (context) => Text('add'),
            builder: (context) =>
                HouseForm(params: settings.arguments as Map<String, dynamic>),
          );
        } else if (settings.name == '/house_detail') {
          return MaterialPageRoute(
            settings: settings, // popUntil 就可以进入动态路由 匹配名字
            // builder: (context) => Text('add'),
            builder: (context) => HouseDetail(
                id: (settings.arguments as Map<String, dynamic>)['id']),
          );
        } else {
          return MaterialPageRoute(
            builder: (context) => NotFound(),
          );
        }
      } else {
        // 没有token
        return MaterialPageRoute(builder: (context) => const LoginPage());
      }
    },
    // initialRoute: "/", //默认路由,第一个页面访问哪一个 ability中 loadContent()
  ));
}
